//
//  MoviesController.m
//  电影er
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MoviesController.h"
#import "MoviesModol.h"
#import "MoviesTableViewCell.h"
#import "MovsendController.h"
#import "ThViewController.h"
@interface MoviesController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,retain)NSMutableArray *muarray;


@end

@implementation MoviesController

- (void)viewDidLoad {
    [_muarray release];
    self.muarray = [[NSMutableArray alloc]init];
    self.modol = [[MoviesModol alloc]init];
    
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.4 blue:0.6 alpha:1];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self creactabview];
    [self shujufenxi];
    self.navigationItem.title = @"电影";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.41f green:0.61f blue:0.68f alpha:1.00f];
    self.navigationController.navigationBar.titleTextAttributes=@{
                                                                  NSForegroundColorAttributeName:[UIColor blackColor],
                                                                  NSFontAttributeName:[UIFont boldSystemFontOfSize:20]
                                                                  
                                                                  };
    
}
-(void)creactabview{
    UITableView *tableview= [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-110) style:UITableViewStylePlain];
    tableview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tableview];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.rowHeight = 200;
    [tableview registerClass:[MoviesTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
}
-(void)shujufenxi{
    
    NSString *str = [[NSBundle mainBundle]pathForResource:@"movielist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:str];
    NSError *error = nil;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray *array = [dic objectForKey:@"result"];
    for (NSDictionary *dict in array) {
       self.modol= [[MoviesModol alloc]init];
        [_modol setValuesForKeysWithDictionary:dict];
        [self.muarray addObject:_modol];
        [_modol release];
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"切换" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.muarray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MoviesTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    self.modol = [self.muarray objectAtIndex:indexPath.row];
    cell.mmodel = self.modol;
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 200;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MovsendController *msvc = [[MovsendController alloc]init];
    self.modol =  [self.muarray objectAtIndex:indexPath.row];
    msvc.modoll =self.modol;
    [self.navigationController pushViewController:msvc animated:YES];
    
}

-(void)rightAction:(UIButton*)button{
    ThViewController *tv= [[ThViewController alloc]init];
    [self.navigationController pushViewController:tv animated:YES];
    
}



@end
